package cn.yunhe.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import cn.yunhe.entity.Book;
import cn.yunhe.service.BookService;

public class BookAction extends BaseAction {
	private int bookid;
	private String bookname;
	private String author;
	private String publish;
	private double price;
	private int count;
	private File picture;//图片
	private String pictureFileName; //文件名
	private String savePath; //路径
	/**
	 * 根据作者查询
	 * @return
	 * @throws Exception
	 */
	public String getBookByAuthor() throws Exception {
		BookService bookService=new BookService();
		List<Book> list=bookService.getBookByAuthor(author);
		if (list!=null&&list.size()>0) {
			getMapSession().put("list", list);
			return "getBookByAuthor_success";
		}else {
			return ERROR;
		}
	}
	/**
	 * 根据库存查询
	 * @return
	 * @throws Exception
	 */
	public String getBookByCount() throws Exception {
		BookService bookService=new BookService();
		List<Book> list=bookService.getBookByCount(count);
		if (list!=null&&list.size()>0) {
			getMapSession().put("list", list);
			return "getBookByCount_success";
		}else {
			return ERROR;
		}
	}
	
	/**
	 * 查询
	 * @return
	 * @throws Exception
	 */
	public String getAllBook() throws Exception {
		BookService bookService=new BookService();
		List<Book> list=bookService.getAllBook();
		if (list!=null&&list.size()>0) {
			getMapSession().put("list", list);
			return "getAllBook_success";
		}else {
			return ERROR;
		}
	}
	
	public String delBook() throws Exception {
		BookService bookService=new BookService();
		Boolean flag=bookService.delBook(bookid);
		if (flag) {
			return "delBook_success";
		}else {
			return ERROR;
		}
	}
	
	/**
	 * 添加书籍
	 * @return
	 * @throws Exception
	 */
	public String addBook() throws Exception {
		BookService bookService=new BookService();
		String uploadPath=getSavePath()+"\\"+getPictureFileName();
		doUpload(picture, uploadPath);	
		String picname=getPictureFileName();
		getMapSession().put("picname", picname);
		Boolean fBoolean=bookService.addBook(bookname,author,publish,price,count,picname);
		if (fBoolean) {
			return "addBook_success";
		}else {
			return ERROR;
		}
	}
	public void doUpload(File picture,String uploadFilePath) {
		try {
			FileOutputStream fos=new FileOutputStream(uploadFilePath);
			FileInputStream fis=new FileInputStream(picture);
			
			byte[] buffer=new byte[1024];
			int len=0;
			
			while ((len=fis.read(buffer))!=-1) {
				fos.write(buffer,0,len);
			}
			fis.close();
			fos.close();
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	
	
	public BookAction() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	
	public int getBookid() {
		return bookid;
	}
	public void setBookid(int bookid) {
		this.bookid = bookid;
	}
	public String getBookname() {
		return bookname;
	}
	public void setBookname(String bookname) {
		this.bookname = bookname;
	}
	public String getAuthor() {
		return author;
	}
	public void setAuthor(String author) {
		this.author = author;
	}
	public String getPublish() {
		return publish;
	}
	public void setPublish(String publish) {
		this.publish = publish;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	public int getCount() {
		return count;
	}
	public void setCount(int count) {
		this.count = count;
	}
	public File getPicture() {
		return picture;
	}
	public void setPicture(File picture) {
		this.picture = picture;
	}
	public String getPictureFileName() {
		return pictureFileName;
	}
	public void setPictureFileName(String pictureFileName) {
		this.pictureFileName = pictureFileName;
	}
	public String getSavePath() {
		return ServletActionContext.getServletContext().getRealPath(savePath);
	}
	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}
	
	
}
